package com.yipao.admin.web.sales;



import com.yipao.admin.annotation.RequiresPermissionsDesc;
import com.yipao.core.util.ResponseUtil;
import com.yipao.core.validator.Sort;
import com.yipao.db.domain.Order;
import com.yipao.db.domain.YipaoAddress;
import com.yipao.db.domain.sales.Sales;
import com.yipao.db.service.order.OrderService;
import com.yipao.db.service.sales.AdminSalesService;

import io.swagger.models.auth.In;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author voyboy
 */
@RestController
@RequestMapping("/admin/sales")
@Validated
public class AdminSalesController {
    private final Log log = LogFactory.getLog(AdminSalesController.class);


    @Resource
    private AdminSalesService adminSalesService;


    /**
     *
     * @param userId
     * @param orderId
     * @param start
     * @param end
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */

    @GetMapping("/list")
    public Object list(@RequestParam(value = "userId",required = false) String userId,
                       @RequestParam(value  = "orderId",required = false) Integer orderId,
                       @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start,
                       @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @com.yipao.core.validator.Order @RequestParam(defaultValue = "desc") String order) {
        List<Order> orders = adminSalesService.selectByExample(userId, orderId, start, end, page, limit, sort, order);
        return ResponseUtil.okList(orders);
    }


    @PostMapping("/add")
    public Object add(@RequestBody Sales sales){
        adminSalesService.add(sales);
        return ResponseUtil.ok(sales);
    }

    @PostMapping("update")
    public Object update(@RequestBody Sales sales){
        adminSalesService.update(sales);
        return ResponseUtil.ok(sales);
    }

    @GetMapping("/delete/{id}")
    public Object delete(@PathVariable("id") Integer id){
        adminSalesService.delete(id);
        return ResponseUtil.ok();
    }




}
